package com.lc.advertising.ui.activity;

import android.content.Intent;
import android.os.Bundle;
import android.text.Html;
import android.view.View;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.listener.OnItemClickListener;
import com.chad.library.adapter.base.viewholder.BaseViewHolder;
import com.hjq.http.EasyHttp;
import com.hjq.http.listener.HttpCallback;
import com.lc.advertising.R;
import com.lc.advertising.app.AppActivity;
import com.lc.advertising.http.request.MessageApi;
import com.lc.advertising.http.response.MeassageBean;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnRefreshLoadMoreListener;

import org.jetbrains.annotations.NotNull;

import java.util.ArrayList;
import java.util.List;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import butterknife.BindView;
import butterknife.ButterKnife;

public class MessageActivity extends AppActivity {
    @BindView(R.id.recyclerview)
    RecyclerView recyclerview;
    @BindView(R.id.refreshLayout)
    SmartRefreshLayout refreshLayout;
    List<MeassageBean.DmessageBean> invirstList = new ArrayList<>();
    private String system_id;
    private int mPage = 1;
    private BaseQuickAdapter productAdapter;
    private int total;
    @Override
    protected int getLayoutId() {
        return R.layout.activity_message;
    }

    @Override
    protected void initView() {

    }

    @Override
    protected void initData() {

    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // TODO: add setContentView(...) invocation
        ButterKnife.bind(this);

        refreshLayout.setOnRefreshLoadMoreListener(new OnRefreshLoadMoreListener() {
            @Override
            public void onLoadMore(final @NonNull RefreshLayout refreshLayout) {
                refreshLayout.getLayout().postDelayed(() -> {

                    if (mPage < total) {
                        mPage++;
                       message_list_more();

                        refreshLayout.finishLoadMore();
                    } else {
                        toast("数据全部加载完毕");
                        refreshLayout.finishLoadMoreWithNoMoreData();//将不会再次触发加载更多事件
                    }
                }, 1000);
            }

            @Override
            public void onRefresh(final @NonNull RefreshLayout refreshLayout) {
                refreshLayout.getLayout().postDelayed(() -> {
                    mPage = 1;
                    message_list();
                    refreshLayout.finishRefresh();
                }, 1000);

            }
        });
        recyclerview.setLayoutManager(new LinearLayoutManager(getContext()));
        productAdapter = new BaseQuickAdapter<MeassageBean.DmessageBean, BaseViewHolder>(R.layout.item_message, invirstList) {
            @Override
            protected void convert(@NotNull BaseViewHolder holder, MeassageBean.DmessageBean databean) {

                holder.setText(R.id.tv_message, Html.fromHtml(databean.getContent()));



            }
        };
        recyclerview.setAdapter(productAdapter);
        productAdapter.setOnItemClickListener(new OnItemClickListener() {
            @Override
            public void onItemClick(@NonNull @NotNull BaseQuickAdapter<?, ?> adapter, @NonNull @NotNull View view, int position) {
                startActivity(new Intent(MessageActivity.this,DanyeActivity.class).putExtra("id",invirstList.get(position).getId()+"").putExtra("title",invirstList.get(position).getTitle()));
            }
        });
        message_list();
    }

    private void message_list_more() {
        EasyHttp.post(this)
                .api(new MessageApi()
                        .setPage(mPage))
                .request(new HttpCallback<MeassageBean>(this) {

                    @Override
                    public void onSucceed(MeassageBean data) {
//
                        if (data.getCode() == 200) {
                            total = data.getTotal_count();
                            invirstList.addAll(data.getDmessage());
                            productAdapter.setNewData(invirstList);
                            productAdapter.notifyDataSetChanged();
                        }

                    }


                });
    }

    private void message_list() {
        EasyHttp.post(this)
                .api(new MessageApi()
                        .setPage(mPage))
                .request(new HttpCallback<MeassageBean>(this) {

                    @Override
                    public void onSucceed(MeassageBean data) {
//
                        if (data.getCode() == 200) {
                            total = data.getTotal_count();
                            invirstList.clear();
                            invirstList.addAll(data.getDmessage());
                            productAdapter.setNewData(invirstList);
                            productAdapter.notifyDataSetChanged();
                        }

                    }


                });
    }
}
